package com.zr.eim.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import com.zr.common.annotation.Anonymous;
import com.zr.common.core.domain.entity.SysUser;
import com.zr.common.utils.SecurityUtils;
import com.zr.common.utils.StringUtils;
import com.zr.eim.domain.EimDataDiagnosticInfo;
import com.zr.eim.domain.EimHomeInformation;
import com.zr.eim.service.IEimDataDiagnosticInfoService;
import com.zr.eim.service.IEimHomeInformationService;
import com.zr.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.util.ThrowableCauseExtractor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zr.common.annotation.Log;
import com.zr.common.core.controller.BaseController;
import com.zr.common.core.domain.AjaxResult;
import com.zr.common.enums.BusinessType;
import com.zr.eim.domain.EimDataPerson;
import com.zr.eim.service.IEimDataPersonService;
import com.zr.common.utils.poi.ExcelUtil;
import com.zr.common.core.page.TableDataInfo;

/**
 * 居民基本信息Controller
 * 
 * @author peihang
 * @date 2022-10-21
 */
@RestController
@RequestMapping("/eim/person")
public class EimDataPersonController extends BaseController
{
    @Autowired
    private IEimDataPersonService eimDataPersonService;

    @Autowired
    private IEimDataDiagnosticInfoService diagnosticInfoService;

    @Autowired
    private IEimHomeInformationService iEimHomeInformationService;

    @Autowired
    private ISysConfigService iSysConfigService;

    /**
     * 查询居民基本信息列表
     */
    @GetMapping("/list")
    @Anonymous
    @PermitAll
    public TableDataInfo list(EimDataPerson eimDataPerson)
    {
        startPage();
        List<EimDataPerson> list = eimDataPersonService.selectEimDataPersonList(eimDataPerson);
        return getDataTable(list);
    }

    /**
     * 查询居民详细信息App端使用
     */
//    @PreAuthorize("@ss.hasPermi('eim:person:list')")
    @GetMapping("/list/app")
    @Anonymous
    @PermitAll
    public AjaxResult listApp(EimDataPerson eimDataPerson) {
        if (StringUtils.isEmpty(eimDataPerson.getPersonId()) || StringUtils.isEmpty(eimDataPerson.getName())){
            List<EimDataPerson> list = null;//明确返回的内容特加
            return AjaxResult.success("查询失败", null);
        }else{
            List<EimDataPerson> list = eimDataPersonService.selectEimDataPersonList1(eimDataPerson);
            return AjaxResult.success("查询成功",list);
        }
    }

/**
     * 查询未管控病例基本信息列表
     */
    @GetMapping("/listPatient")
    public AjaxResult listPatient()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<EimDataPerson> list = eimDataPersonService.selectPatientList(user.getUserName());
        return AjaxResult.success("查询成功",list);
    }

    @GetMapping("/listDiagnosticInfo")
    public TableDataInfo listDiagnosticInfo(EimDataPerson eimDataPerson)
    {
        startPage();
        List<EimDataPerson> list = eimDataPersonService.selectDiagnosticInfoList(eimDataPerson);
        return getDataTable(list);
    }




    /**
     * 查询居民及核酸基本信息列表
     */

    @GetMapping("/rlist")
    @Anonymous
    @PermitAll
    public TableDataInfo rlist(EimDataPerson eimDataPerson)
    {
        startPage();
        List<EimDataPerson> list = eimDataPersonService.selectEimDataPersonList1(eimDataPerson);
        return getDataTable(list);
    }


    /**
     * 导出居民基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('eim:person:export')")
    @Log(title = "居民基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EimDataPerson eimDataPerson)
    {
        List<EimDataPerson> list = eimDataPersonService.selectEimDataPersonList(eimDataPerson);
        ExcelUtil<EimDataPerson> util = new ExcelUtil<EimDataPerson>(EimDataPerson.class);
        util.exportExcel(response, list, "居民基本信息数据");
    }

    /**
     * 获取居民基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('eim:person:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(eimDataPersonService.selectEimDataPersonById(id));
    }

    /**
     * 新增居民基本信息
     */
    @PostMapping("/add/app")
    @Anonymous
    @PermitAll
    public AjaxResult addApp(@RequestBody EimDataPerson eimDataPerson)
    {
        System.out.println(eimDataPerson);
        if(StringUtils.isEmpty(eimDataPerson.getName()) || StringUtils.isEmpty(eimDataPerson.getPersonId()) || eimDataPerson.getIfDiagnosis() == null){
            return AjaxResult.success("必填信息不能为空");
        }else if((eimDataPersonService.selectEimDataPersonList1(eimDataPerson)).size() == 0){
            eimDataPersonService.insertEimDataPerson(eimDataPerson);
            return AjaxResult.success("新增成功");
        }else {
            eimDataPerson.setId(eimDataPersonService.selectEimDataPersonList1(eimDataPerson).get(0).getId());
            eimDataPersonService.updateEimDataPerson(eimDataPerson);
            return AjaxResult.success("修改成功");
        }
    }

    /**
     * 新增居民基本信息
     */
    @Log(title = "居民基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    @Anonymous
    @PermitAll
    public AjaxResult add(@RequestBody EimDataPerson eimDataPerson)
    {
        return toAjax(eimDataPersonService.insertEimDataPerson(eimDataPerson));
    }

    /**
     * 修改居民基本信息
     */
    @PreAuthorize("@ss.hasPermi('eim:person:edit')")
    @Log(title = "居民基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EimDataPerson eimDataPerson)
    {
        return toAjax(eimDataPersonService.updateEimDataPerson(eimDataPerson));
    }

    /**
     * 删除居民基本信息
     */
    @PreAuthorize("@ss.hasPermi('eim:person:remove')")
    @Log(title = "居民基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eimDataPersonService.deleteEimDataPersonByIds(ids));
    }

    /**
     * 转为确诊
     */
    @PreAuthorize("@ss.hasPermi('eim:person:diagnosis')")
    @Log(title = "转为确诊病例", businessType = BusinessType.UPDATE)
    @PostMapping("/toDiagnosis/{id}")
    public AjaxResult toDiagnosis(@PathVariable Long id)
    {
        EimDataPerson eimPerson =eimDataPersonService. selectEimDataPersonById(id);
        EimDataDiagnosticInfo diagnosticInfo = new EimDataDiagnosticInfo();
        diagnosticInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserName());
        diagnosticInfo.setPersonId(eimPerson.getPersonId());
        diagnosticInfo.setControlLimitTime(new Date());
        diagnosticInfo.setCreateTime(new Date());
        diagnosticInfo.setRemark("确诊病例");
        diagnosticInfo.setCheckId(eimPerson.getLastCheckId());
        diagnosticInfoService.insertEimDataDiagnosticInfo(diagnosticInfo);
        return toAjax(eimDataPersonService.eimDataPersonToDiagnosisByIds(id));
    }
    /**
     * 转为居家监测
     */
    @PreAuthorize("@ss.hasPermi('eim:person:monitor')")
    @Log(title = "转为居家监测", businessType = BusinessType.UPDATE)
    @PostMapping("/toMonitor/{id}")
    public AjaxResult toMonitor(@PathVariable Long id)
    {
        EimDataPerson eimPerson =eimDataPersonService. selectEimDataPersonById(id);
        EimHomeInformation homeInformation = new EimHomeInformation();
        homeInformation.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserName());
        homeInformation.setMonitorBeginTime(new Date());
        homeInformation.setMonitorEndTime(new Date(new Date().getTime() + Integer.parseInt(iSysConfigService.selectConfigByKey("eim.monitor.day")) * 24 * 60 * 60 * 1000));
        homeInformation.setIdCard(eimPerson.getPersonId());
        homeInformation.setStatus(0L);
        iEimHomeInformationService.insertEimHomeInformation(homeInformation);
        return toAjax(eimDataPersonService.eimDataPersonToMonitorByIds(id));
    }

    /**
     * 查询工作人员数量
     * 需求模块：数据大屏
     */
    @PreAuthorize("@ss.hasPermi('eim:person:query')")
    @PostMapping("/getAllMedicalPersonnel")
    public AjaxResult getAllMedicalPersonnel()
    {
        return AjaxResult.success(eimDataPersonService.getAllMedicalPersonnel());
    }


}
